package com.bawei.demo_shoppingcar.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.bean.ShoppingDataBean;

import org.xutils.x;

import java.util.List;

/**
 * Created by HP on 2017/3/25.
 */

public class MyRecyclerAdapterTop extends RecyclerView.Adapter<MyRecyclerAdapterTop.OneMyViewHolder> {

    private final LayoutInflater inflater;
    private List<ShoppingDataBean.DataBean.BestSellersBean.GoodsListBeanX> goodsList;
    public MyRecyclerAdapterTop(Context context,List<ShoppingDataBean.DataBean.BestSellersBean.GoodsListBeanX> goodsList){
        inflater = LayoutInflater.from(context);
        this.goodsList=goodsList;
    }

    MyHomeDefaultItemClickListener myHomeDefaultItemClickListener;
    /**
     * 创建default商品的回调接口
     */
    public interface MyHomeDefaultItemClickListener {
        void onItemClick(View view, int position);
    }

    //在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
    public void setDefaultItemClickListener(MyHomeDefaultItemClickListener myItemClickListener) {
        myHomeDefaultItemClickListener = myItemClickListener;
    }


    @Override
    public OneMyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View inflate = inflater.inflate(R.layout.one_my_item,viewGroup,false);
        return new OneMyViewHolder(inflate,myHomeDefaultItemClickListener);
    }

    @Override
    public void onBindViewHolder(OneMyViewHolder holder, int i) {
        x.image().bind(holder.one_my_imageView,goodsList.get(i).getGoods_img());
        holder.one_my_item_textView.setText(goodsList.get(i).getGoods_name());
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    class OneMyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private final TextView one_my_item_textView;
        private final ImageView one_my_imageView;
        MyHomeDefaultItemClickListener myHomeDefaultItemClickListener;
        public OneMyViewHolder(View itemView, MyHomeDefaultItemClickListener myHomeDefaultItemClickListener) {
            super(itemView);
            one_my_item_textView = (TextView) itemView.findViewById(R.id.one_my_item_textView);
            one_my_imageView = (ImageView) itemView.findViewById(R.id.one_my_imageView);
            this.myHomeDefaultItemClickListener = myHomeDefaultItemClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (myHomeDefaultItemClickListener != null) {
                myHomeDefaultItemClickListener.onItemClick(v, getPosition());
            }
        }
    }
}
